#!/usr/bin/perl

use strict;

my $ISOC_URL = "http://www.isoc.org/worldipv6/participants/participants.xml";
my $RIPE_URL = "http://albatross.ripe.net/6to4relaytest/relays.txt";
my $LOCAL_NAMES = "local-names.txt";

# Gather names from the web
my @isoc_names = `curl -s "$ISOC_URL" | grep "v4_only\\|v6_only\\|hostname" | sed "s/ //g; s/<[^>]*>//g" | sort | uniq`;
my @ripe_names = `curl -s "$RIPE_URL" | sed "s/#.*//g; s/ .*//g" | grep -v "^[ ]*\$" | sort | uniq`;
chomp @isoc_names;
chomp @ripe_names;

# Get a /48 for each name
my %names = ();
foreach my $name (@isoc_names) { 
    my $aaaa48 = `dig +short $name AAAA | grep -v [g-z] | head -n 1 | sed -e 's/\\(^[^:]*:[^:]*:[^:]*:\\).*\$/\\1/;'`;
    chomp $aaaa48;
    $names{$name} = $aaaa48; 
}
foreach my $name (@ripe_names) { 
    my $aaaa48 = `dig +short $name AAAA | grep -v [g-z] | head -n 1 | sed -e 's/\\(^[^:]*:[^:]*:[^:]*:\\).*\$/\\1/;'`;
    chomp $aaaa48;
    $names{$name} = $aaaa48; 
}

print `cat $LOCAL_NAMES`;
foreach my $name (keys %names) {
    printf "%s|%s\n", $name, $names{$name};
}


